#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：HiflyVideoAPI_Test 
@File    ：test_case_v1_voices.py
@Author  ：beibei
@Date    ：7/16/25 2:00 PM 
'''
import unittest

from business.voices.vo import BusinessRe
from common.assertCommon import AssertCommon
from common.caseOutput import info, class_case_decoration
from common.yamlLoader import YamlRead
from utils.utils_base import save_json_response
from utils.utils_file import load_json_from_project
from datetime import datetime


@class_case_decoration
class TestCaseV1CreateCosyVoices(unittest.TestCase):
    """
    测试用例：测试v1——cosy声音语音相关接口
    """

    ac = AssertCommon()
    env_config = YamlRead().env_config1()
    host = env_config['host']
    br = BusinessRe()
    authorization = env_config['Authorization']

    def testCase01_v1_voices(self):
        """创建cosy声音"""
        info('【step】获取URL地址')
        url = self.host + f'/api/v1/hifly/voice/create'
        body = {
            "type": 8
        }
        res = self.br.post(url, self.authorization, body)
        info('【step】断言 code == 0')
        self.ac.code_assert(0, res.json()["code"])
        save_json_response(res.json(), "data/response/voice/v1/voice_create_v1_8.json")

    def testCase02_v1_voices_train(self):
        """训练cosy声音"""
        info('【step】调用创建高保真声音接口获取voice_id')
        data = load_json_from_project("data/response/voice/v1/voice_create_v1_8.json")
        voice_id = data["voice_id"]
        info(f'【step】获取到voice_id: {voice_id}')
        url = self.host + f'/api/v1/hifly/voice/train'
        body = {
            "voice_id": voice_id,
            "audio_url": "https://shortvideo-cdn-dev.lingverse.co/hf/local/37/audios/10s_audios.MP3",
            "voice_type": 8
        }
        info(f'【step】请求训练接口: {url}, body: {body}')
        res = self.br.post(url, self.authorization, body,timeout=30)
        info('【step】断言 code == 0')
        self.ac.code_assert(0, res.json()["code"])

    def testCase03_v1_voices_train(self):
        """确认cosy声音"""
        info('【step】调用创建高保真声音接口获取voice_id')
        data = load_json_from_project("data/response/voice/v1/voice_create_v1_8.json")
        voice_id = data["voice_id"]
        info(f'【step】从json文件获取voice_id: {voice_id}')
        url = self.host + f'/api/v1/hifly/voice/confirm'
        body = {
            "voice_id": voice_id,
            "title": "cosy" + datetime.now().strftime("%Y%m%d_%H%M%S"),
            "voice_type": 8
        }
        info(f'【step】请求确认接口: {url}, body: {body}')
        res = self.br.post(url, self.authorization, body)
        info('【step】断言 code == 0')
        self.ac.code_assert(0, res.json()["code"])
